﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Windows;
using Galaktika.BI.Extensibility.Dom;
using Galaktika.BI.Silverlight.Controls;

namespace Galaktika.BI.Silverlight.ClientApp.Items.Toolbar
{
    public static class ToolbarItemFactory
    {
        public static UIElement CreateItem(BrickComponent component)
        {
            switch (component.ComponentId)
            {
                case "{3457ACE1-460D-47ee-B597-721C595F4A43}":
                    return CreateSessionStateButton(component);
                case "{DA180CBE-5200-4173-9802-3713D4BEFD2D}":
                    return CreateSimpleButton(component);
                case "{44D9E272-CE06-46f1-9429-5503C9C788C0}":
                    return CreateLabel(component);
                case "{3E73537F-F95D-4292-BD11-EDAECA41283E}":
                    return CreateSelector(component);
                case "{DA63E520-089E-43ce-874C-D5C994CF4ABD}":
                    return CreateSwitchButton(component);
                default:
                    return null;
            }
        }

        public static bool IsBeginGroup(BrickComponent component)
        {
            return component.UserData.ContainsKey("ABeginGroup") ?
                (bool)component.UserData["ABeginGroup"] :
                false;
        }

        private static SessionStateButton CreateSessionStateButton(BrickComponent component)
        {
            var btn = new SessionStateButton();
            btn.SessionStateAction = 
                component.UserData.ContainsKey("SessionStateAction") ? 
                (Galaktika.BI.DataManagement.SessionStateAction)Enum.Parse(typeof(Galaktika.BI.DataManagement.SessionStateAction), (string)component.UserData["SessionStateAction"], false) :
                Galaktika.BI.DataManagement.SessionStateAction.None;
            if (component.UserData.ContainsKey("AConnectionName"))
            {
                btn.AConnectionName = component.UserData["AConnectionName"] as string;
            }
            if (component.UserData.ContainsKey("AUseConfirmation"))
            {
                btn.AUseConfirmation = (bool)component.UserData["AUseConfirmation"];
            }
            if (component.UserData.ContainsKey("AConfirmationText"))
            {
                btn.AConfirmationText = (string)component.UserData["AConfirmationText"];
            }

            FillSimpleButtonProperties(btn, component);

            return btn;
        }

        private static SimpleButton CreateSimpleButton(BrickComponent component)
        {
            var btn = new SimpleButton();
            FillSimpleButtonProperties(btn, component);
            return btn;
        }

        private static void FillSimpleButtonProperties(SimpleButton btn, BrickComponent component)
        {
            if (component.UserData.ContainsKey("AText"))
            {
                btn.AText = component.UserData["AText"] as string;
            }
            btn.AShowToolTips = 
                component.UserData.ContainsKey("AShowToolTips") ?
                (bool)component.UserData["AShowToolTips"] :
                false;
            if (component.UserData.ContainsKey("AToolTipText"))
            {
                btn.AToolTipText = component.UserData["AToolTipText"] as string;
            }
            if (component.UserData.ContainsKey("AActionInvoker"))
            {
                btn.AActionInvoker = component.UserData["AActionInvoker"] as BrickActionInvoker;
            }
            if (component.UserData.ContainsKey("AImageAssembly") &&
                component.UserData.ContainsKey("AImageName"))
            {
                btn.ABitmapImage = UriResources.GetImage(
                    (string)component.UserData["AImageAssembly"],
                    (string)component.UserData["AImageName"]);
            }
        }

        private static LabelControl CreateLabel(BrickComponent component)
        {
            var lbl = new LabelControl();
            if (component.UserData.ContainsKey("AText"))
            {
                lbl.AText = component.UserData["AText"] as string;
            }
            if (component.UserData.ContainsKey("AFormatScript"))
            {
                lbl.AFormatScript = component.UserData["AFormatScript"] as string;
            }
            lbl.AShowToolTips =
                component.UserData.ContainsKey("AShowToolTips") ?
                (bool)component.UserData["AShowToolTips"] :
                false;
            if (component.UserData.ContainsKey("AToolTipText"))
            {
                lbl.AToolTipText = component.UserData["AToolTipText"] as string;
            }
            return lbl;
        }

        private static Selector CreateSelector(BrickComponent component)
        {
            var sel = new Selector();

            if (component.UserData.ContainsKey("ADataSource"))
            {
                sel.ADataSource = component.UserData["ADataSource"];
            }
            if (component.UserData.ContainsKey("AVariable"))
            {
                sel.AVariable = component.UserData["AVariable"] as string;
            }
            if (component.UserData.ContainsKey("APath"))
            {
                sel.APath = component.UserData["APath"] as string;
            }
            if (component.UserData.ContainsKey("ANameItem"))
            {
                sel.ANameItem = component.UserData["ANameItem"] as string;
            }
            if (component.UserData.ContainsKey("AValueItem"))
            {
                sel.AValueItem = component.UserData["AValueItem"] as string;
            }

            return sel;
        }

        private static SwitchButton CreateSwitchButton(BrickComponent component)
        {
            var btn = new SwitchButton();
            if (component.UserData.ContainsKey("AText"))
            {
                btn.AText = component.UserData["AText"] as string;
            }
            btn.AShowToolTips =
                component.UserData.ContainsKey("AShowToolTips") ?
                (bool)component.UserData["AShowToolTips"] :
                false;
            if (component.UserData.ContainsKey("AToolTipText"))
            {
                btn.AToolTipText = component.UserData["AToolTipText"] as string;
            }
            if (component.UserData.ContainsKey("AVariable"))
            {
                btn.AVariable = component.UserData["AVariable"] as string;
            }
            btn.AValueMask =
                component.UserData.ContainsKey("AValueMask") ?
                (int)component.UserData["AValueMask"] :
                0;
            btn.AInGroup =
                component.UserData.ContainsKey("AInGroup") ?
                (bool)component.UserData["AInGroup"] :
                false;
            if (component.UserData.ContainsKey("AAssemblyPush"))
            {
                btn.AAssemblyPush = component.UserData["AAssemblyPush"] as string;
            }
            if (component.UserData.ContainsKey("AAssemblyRelease"))
            {
                btn.AAssemblyRelease = component.UserData["AAssemblyRelease"] as string;
            }
            if (component.UserData.ContainsKey("AResNamePush"))
            {
                btn.AResNamePush = component.UserData["AResNamePush"] as string;
            }
            if (component.UserData.ContainsKey("AResNameRelease"))
            {
                btn.AResNameRelease = component.UserData["AResNameRelease"] as string;
            }
            return btn;
        }
    }
}
